package com.zmn.plat.admin.controller.part.unit;

import java.util.Date;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartSku;
import com.zmn.plat.model.entity.part.PartSkuUnit;

/**
 * 类描述： PartSkuUnitModifyController
 *
 * @author XiangJunMing
 * @since 2019/05/28 20:36
 */
@RestController
@RequestMapping("/part/unit")
public class PartSkuUnitModifyController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartSkuUnitModifyController.class);

    /**
     * 描述: 添加配件单位换算
     *
     * @author XiangJunMing
     * @since 2019/03/23 20:34
     */
    @RequestMapping(value = {"ajax/addEdit"})
    @RequiresPermissions("/part/unit/addEdit.action")
    public ResponseDTO doAddEdit(@RequestBody PartSkuUnit entity) {
        PartSku byKey = partSkuService.findByKey(entity.getSkuId());
        entity.setName(byKey.getName());
        LoginStaff mcStaff = getMcStaff();
        entity.setCreater(mcStaff.getRealName());
        entity.setUpdater(mcStaff.getRealName());
        entity.setUpdateTime(new Date());

        partSkuUnitService.insert(entity);
        return ResponseDTO.success(true,"添加配件单位换算成功！");
    }

    /**
     * 描述: 删除配件单位换算
     *
     * @author XiangJunMing
     * @since 2019/05/28 20:34
     */
    @RequestMapping(value = {"ajax/del"})
    @RequiresPermissions("/part/unit/del.action")
    public ResponseDTO del(Integer skuId) {

        if (skuId == null || skuId == 0) {
            return ResponseDTO.fail("skkuId不能为空！");
        }

        PartSkuUnit byKey = partSkuUnitService.findByKey(skuId);
        if (byKey == null || byKey.getSkuId() == null || !byKey.getSkuId().equals(skuId)) {
            return ResponseDTO.fail("删除失败！数据不存在！");
        }

        partSkuUnitService.deleteByKey(skuId);

        return ResponseDTO.success(true,"删除配件单位换算成功！");
    }

}
